<?php
/**
 * 用户处理model
 * Created by Forever.
 * User: Forever
 * Date: 2018/10/03
 * Time: 上午 10:39
 */
namespace app\pay\model;
use think\Cache;
use \think\Model;
use \think\Db;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
class Pay extends Model{
    protected $easywechat;
    protected $options;
    public function initialize() {
    }


    public static function payment(){

        $options = config('wechat.options');
        $options['payment']['merchant_id']= "1465233002";//商户号
        // $payment = $options['payment'];
        $easywechat = new Application($options);
        // $no = 'jub'.time();
        $Nonumber = self::buildOrderNo();
        $attributes = [
            'trade_type'       => 'JSAPI', // JSAPI，NATIVE，APP...
            'body'             => 'iPad mini 16G 白色',
            'detail'           => 'iPad mini 16G 白色',
            'out_trade_no'     => $Nonumber,
            'total_fee'        => '1', // 单位：分
            'notify_url'       => 'http://xgapi.xinpear.net/pay/Pay_Return/pay_returns/web_type/22', // 支付结果通知网址，如果不设置则会使用配置里的默认地址
            'openid'           => 'ojQ6a5SfEqhKCifDcC_wx4KIEfVM', // trade_type=JSAPI，此参数必传，用户在商户appid下的唯一标识，
        ];
        $order = new Order($attributes);
        $result = $easywechat->payment->prepare($order);
        $outcome['appid'] = $result->appid;
        $outcome['mch_id'] = $result->mch_id;
        $outcome['device_info'] = $result->device_info;
        $outcome['nonce_str'] = $result->nonce_str;
        $outcome['sign'] = $result->sign;
        $outcome['signType'] = $result->signType;
        $outcome['result_code'] = $result->result_code;
        $outcome['notify_url'] = 'http://xgapi.xinpear.net/pay/Pay_Return/pay_returns/web_type/22222';
        //$outcome['seller_key'] = $this->options['payment']['key'];

        $info = $easywechat->payment->configForPayment($result->prepay_id,false);
        // dump($info);die;

        $outcome['prepay_id'] = $result->prepay_id;
        $outcome['sign'] = $info['paySign'];
        $outcome['nonce_str'] = $info['nonceStr'];
        $outcome['times'] = $info['timeStamp'];
        $outcome['prepay_id'] = $info['package'];

      return $outcome;

    }

  public static function buildOrderNo(){
    list($usec, $sec) = explode(" ", microtime());
    $usec = substr(str_replace('0.', '', $usec), 0 ,4);
    $str  = self::rand_string(2,1);
   return 'O'.date("YmdHis").$usec.$str;

  }
  public static  function rand_string($len=6,$type='',$addChars='') {
    $str ='';
    switch($type) {
        case 0:
            $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.$addChars;
            break;
        case 1:
            $chars= str_repeat('0123456789',3);
            break;
        case 2:
            $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ'.$addChars;
            break;
        case 3:
            $chars='abcdefghijklmnopqrstuvwxyz'.$addChars;
            break;
       default :
            // 默认去掉了容易混淆的字符oOLl和数字01，要添加请使用addChars参数
            $chars='ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'.$addChars;
            break;
    }
    if($len>10 ) {//位数过长重复字符串一定次数
        $chars= $type==1? str_repeat($chars,$len) : str_repeat($chars,5);
    }
    if($type!=4) {
        $chars   =   str_shuffle($chars);
        $str     =   substr($chars,0,$len);
    }else{
        // 中文随机字
        for($i=0;$i<$len;$i++){
            $str.= msubstr($chars, floor(mt_rand(0,mb_strlen($chars,'utf-8')-1)),1);
        }
    }
    return $str;
  }

}
